using System;
using SharpMud.Server.Systems.Net;
using SharpMud.MudLib.SAMPLE.Net;
using SharpMud.Net;

namespace SharpMud.MudLib.SAMPLE.Net.SessionStates
{
	public abstract class SessionStateBase : ISessionState<TelnetPlayerSession>
	{
		#region Private Variables
		private bool										_StillActive;
		private System.Collections.GenericQueue<byte>	    _OutgoingDataQueue;
		private Net.TelnetPlayerSession						_Session;
		private ISessionState<TelnetPlayerSession>			_ParentState;
		#endregion

		protected SessionStateBase()
		{
			this._StillActive = true;
			this._OutgoingDataQueue = new System.Collections.GenericQueue<byte>();
			_Session = null;
		}

		protected void SetInactive()
		{
			this._StillActive = false;
		}

		protected Net.TelnetPlayerSession Session
		{
			get
			{
				return _Session;
			}
		}

		protected MudLib.SAMPLE.MudLibRoot MudServer
		{
			get
			{
				if(this.Session==null)
					return null;
				return Session.HostMud;
			}
		}

		#region ISessionState Members

		public virtual void Initialize(TelnetPlayerSession session)
		{
            this.Initialize(session, null);
		}

		void SharpMud.Net.ISessionState.Pulse()
		{
			this.Pulse();
		}

		public virtual void Pulse()
		{
		}

		public virtual SharpMud.Net.ISessionState ParentState
		{
			get
			{
				return this._ParentState;
			}
		}

        //public System.Collections.IQueue<byte> OutgoingDataQueue
        //{
        //    get
        //    {
        //        return _OutgoingDataQueue;
        //    }
        //}

        string SharpMud.Net.ISessionState.StateID
		{
			get
			{
				return this.StateID;
			}
		}
		
		public abstract string StateID {get;}

		public virtual bool StillActive
		{
			get
			{
				return _StillActive;
			}
		}

		#endregion

		#region IDisposable Members

		void IDisposable.Dispose()
		{
			this.Dispose();
		}

		public virtual void Dispose()
		{
		}

		#endregion

        #region ISessionState<TelnetPlayerSession> Members
        public virtual void Initialize(TelnetPlayerSession session, ISessionState<TelnetPlayerSession> parentState)
        {
            this._ParentState = parentState;
            this._Session = session;
        }

        void SharpMud.Net.ISessionState.Initialize(SharpMud.Net.IHostedConnectionHandler hostedConnectionHandler, SharpMud.Net.ISessionState parentState)
        {
            this.Initialize((hostedConnectionHandler as TelnetPlayerSession), (parentState as SharpMud.Net.ISessionState<TelnetPlayerSession>));
        }

        ISessionState<TelnetPlayerSession> ISessionState<TelnetPlayerSession>.ParentState
        {
            get { return _ParentState; }
        }

        #endregion

        #region ISessionState Members

        public void Initialize(IHostedConnectionHandler session, ISessionState parentState)
        {
            this.Initialize(session as TelnetPlayerSession, parentState as ISessionState<TelnetPlayerSession>);
        }

        #endregion
    }
}